package facilius.controller.action.turma;

import facilius.model.ServiceLocator;
import facilius.model.UserConstants;
import facilius.model.pojo.Turma;
import facilius.model.pojo.Usuario;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.mentawai.core.BaseAction;

/**
 *
 * @author Graciano
 */
public class TurmaShowFormAction extends BaseAction {

    @Override
    public String execute() throws Exception {
        String consequence = ERROR;
        String formAction = "TurmaCreate.mtw";
        this.preload();
        try {
            Long id = input.getLong("id");
            if (id != null && id > 0) {
                Turma turma = ServiceLocator.getTurmaService().readById(id);
                output.setValue("id", turma.getId());
                output.setValue("ano", turma.getAno());
                output.setValue("descricao", turma.getDescricao());
                output.setValue("disciplinaId", turma.getDisciplina().getId());
                output.setValue("professorId", turma.getProfessor().getId());
                formAction = "TurmaUpdate.mtw";
            }
            output.setValue("formAction", formAction);
            consequence = SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return consequence;
    }

    public void preload() throws Exception {
        Map<String, Object> criteria = new HashMap<String, Object>();
        Map<String, String> options = ServiceLocator.getDisciplinaService().getOptions(criteria);
        output.setValue("disciplinaOptions", options);

        criteria = new HashMap<String, Object>();
        criteria.put("tipo", UserConstants.PROFESSOR);
        List<Usuario> list = ServiceLocator.getUsuarioService().readByCriteria(criteria);

        Map<String, Object> professores = new HashMap<String, Object>();
        for (int i = 0; i < list.size(); i++) {
            professores.put(list.get(i).getId().toString(), list.get(i));
        }
        output.setValue("professorOptions", professores);
    }
}
